package makabakaquestionnaire.zutsoftware.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import makabakaquestionnaire.zutsoftware.constant.ProjectRedisKeyConstants;
import makabakaquestionnaire.zutsoftware.constant.Result;
import makabakaquestionnaire.zutsoftware.entity.ProjectItem;
import makabakaquestionnaire.zutsoftware.entity.request.SortProjectItemRequest;
import makabakaquestionnaire.zutsoftware.service.impl.ProjectItemServiceImpl;
import makabakaquestionnaire.zutsoftware.utils.redis.RedisUtils;
import makabakaquestionnaire.zutsoftware.utils.SortUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Api(tags = "问题接口")
@RestController
@RequestMapping("project/item")
public class ProjectItemController {

    @Autowired
    SortUtils sortUtils;
    ProjectItemServiceImpl projectItemService;
    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    public void setProjectItemService(ProjectItemServiceImpl projectItemService) {
        this.projectItemService = projectItemService;
    }

    @ApiOperation("根据问卷标识ProKey查询问卷的所有问题")
    @GetMapping("/list/{ProKey}")
    public Result queryItemByProKey(@PathVariable String ProKey) {
        return Result.success(projectItemService.queryProjectItemByProKey(ProKey));
//     int a=   ProjectItemTypeEnum.COUNT.getValue();
    }

    @ApiOperation("创建问题，默认的排序值为所以问题的排序值最大值+间隔66536L")
    @PostMapping("/create")
    public Result insertProjectItem(@RequestBody ProjectItem projectItem) {
        String now = DateUtil.now();
        Date date = DateUtil.parse(now);   //转为date格式
        projectItem.setCreateTime(date);
        projectItem.setUpdateTime(date);
        if (!redisUtils.exists(
                StrUtil.format(ProjectRedisKeyConstants.PROJECT_ITEM_POS_DELTA, projectItem.getProjectKey()))
        ) {
            redisUtils.set(StrUtil.format(ProjectRedisKeyConstants.PROJECT_ITEM_POS_DELTA, projectItem.getProjectKey()),
                    projectItemService.getMaxSort(projectItem.getProjectKey()));
        }
        projectItem.setSort(sortUtils.getInitialSortPosition(
                StrUtil.format(ProjectRedisKeyConstants.PROJECT_ITEM_POS_DELTA, projectItem.getProjectKey())
        ));
        projectItemService.insertProjectItem(projectItem);
        return Result.success(projectItemService.getMaxId());
    }

    @ApiOperation("传入问题的id和前一个问题的排序值和后一个问题的排序值")
    @PostMapping("/sort")
    private Result sortProjectItem(@RequestBody SortProjectItemRequest sortProjectItemRequest) {
        Long sort = sortUtils.calcSortPosition(sortProjectItemRequest.getBeforePosition(), sortProjectItemRequest.getAfterPosition());
        ProjectItem projectItem = projectItemService.queryItemById(sortProjectItemRequest.getId());
        projectItem.setSort(sort);
        projectItemService.updateProject(projectItem);
        return Result.success(projectItem.getId(), "item问题更新成功");
    }

    @ApiOperation("更新问题，将问题要更改的属性传入后端")
    @PostMapping("/update")
    private Result updateProjectItem(@RequestBody ProjectItem projectItem) {
        ProjectItem oldItem = projectItemService.queryItemById(projectItem.getId());
        String now = DateUtil.now();
        Date date = DateUtil.parse(now);
        projectItem.setUpdateTime(date);
        boolean update = false;
        if (ObjectUtil.isNotEmpty(oldItem)) {
            update = projectItemService.updateProject(projectItem);
        }
        return Result.success(projectItem, "item问题更新" + update);
    }

    @ApiOperation("通过Id获得问题")
    @GetMapping("/get/{id}")
    private Result getItemById(@PathVariable Long id) {
        ProjectItem item = projectItemService.queryItemById(id);
        return Result.success(item);

    }

    @GetMapping("/delete/{id}")
    private Result deleteProjectItem(@PathVariable Long id) {
        return Result.success(projectItemService.deleteItem(id), "删除成功");
    }

}
